/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 17655
 * Date: 2025-01-07
 * Time: 15:14
 */
public class test4 {
    public String longestCommonPrefix(String[] strs) {
        // 先把第一个单词拿出来，后面的单词依次比较然后记录与每个最短的前缀。
        String tmp = strs[0];
        for(int i=1;i<strs.length;i++){
            tmp = getCommon(tmp,strs[i]);
        }
        return tmp;
    }

    public String getCommon(String s1,String s2){
        StringBuilder sb = new StringBuilder();
        int i=0;
        while(i<s1.length() && i<s2.length() && s1.charAt(i)==s2.charAt(i)){
            sb.append(s1.charAt(i));
            i++;
        }
        return sb.toString();
    }
}
